package hiatao.puzzle.activity;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

import com.facebook.rebound.ToggleButton;
import com.facebook.rebound.ToggleButton.OnToggleChanged;

import java.text.DecimalFormat;

import haitao.puzzle.R;
import hiatao.puzzle.config.MyApplication;
import hiatao.puzzle.utils.FileUtils;
import hiatao.puzzle.utils.StatusBarCompat;
import hiatao.puzzle.utils.Tools;

public class SettingActivity  extends Activity implements OnClickListener{

    private View top_back;
    private ToggleButton toggle;
    private View suggest_ly;
    private View clean_ly;
    private View update_ly;
    private View about_ly;
    private Button exit_btn;
    private TextView total_tv;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_setting);
		findView();
		init();
		StatusBarCompat.compat(this, getResources().getColor(R.color.primary));
	}

	private void init() {
		top_back.setOnClickListener(this);
		top_back.setOnClickListener(this);
		suggest_ly.setOnClickListener(this);
		clean_ly.setOnClickListener(this);
		update_ly.setOnClickListener(this);
		about_ly.setOnClickListener(this);
		exit_btn.setOnClickListener(this);
		if(MyApplication.getInstance().user!=null){
			exit_btn.setVisibility(View.VISIBLE);
		}
		
		if("0".equals(Tools.getValues("push_on"))|| TextUtils.isEmpty(Tools.getValues("push_on"))){
			toggle.setToggleOn();
		}
		toggle.setOnToggleChanged(new OnToggleChanged() {
			
			@Override
			public void onToggle(boolean on) {
				if(on){
					Tools.saveValues("push_on", "0");
				}else{
					Tools.saveValues("push_on", "1");
				}
			}
		});
		
		double size = FileUtils.getDirSize( FileUtils.getAppPath());
		DecimalFormat df = new java.text.DecimalFormat("#0.0");
		total_tv.setText(df.format(size)+"M");
	}

	private void findView() {
        top_back =  findViewById(R.id.top_back);
        toggle = (com.facebook.rebound.ToggleButton) findViewById(R.id.toggle);
        suggest_ly =  findViewById(R.id.suggest_ly);
        clean_ly =  findViewById(R.id.clean_ly);
        update_ly =  findViewById(R.id.update_ly);
        about_ly =  findViewById(R.id.about_ly);
        exit_btn = (Button) findViewById(R.id.exit_btn);
        total_tv = (TextView) findViewById(R.id.total_tv);
        
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {

		case R.id.top_back:
			finish();
			break;
		case R.id.suggest_ly:
			Tools.goActivity(SettingActivity.this, SuggestActivity.class, null,null);
			break;
		case R.id.clean_ly:
			clean();
			break;
		case R.id.update_ly:
			update();
			break;
		case R.id.about_ly:
			Tools.goActivity(SettingActivity.this, AboutActivity.class, null,null);
			break;
		case R.id.exit_btn:
			exit();
			break;

		default:
			break;
		}
	}

	private void exit() {
		// TODO Auto-generated method stub
		MyApplication.getInstance().user=null;
		MyApplication.getInstance().isLogin=false;
		MyApplication.getInstance().hasChage=true;
		exit_btn.setVisibility(View.GONE);
	}

	private void update() {
		Tools.showWaitDialog(this, "检测中", false);
		new Handler().postDelayed(new Runnable() {
			
			@Override
			public void run() {
				Tools.dismissWaitDialog();
				Tools.showToast("已是最新版本");
			}
		},1200);
		
	}
	private void clean() {
		Tools.showWaitDialog(this, "清理中", false);
		FileUtils.deleteFile(FileUtils.getAppPath());
		total_tv.setText("0M");
		Tools.dismissWaitDialog();
		
	}
	
}
